package com.lej.pay.utils

import android.app.Activity
import android.content.Context
import com.alipay.sdk.app.PayTask
import com.lej.club.wxapi.WXPayEntryActivity
import com.lej.core.utils.extention.toast
import com.lej.wx.PlatformUtil
import com.lej.wx.data.WxStatus
import com.tencent.mm.opensdk.modelpay.PayReq
import com.tencent.mm.opensdk.openapi.WXAPIFactory
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

object PayUtils {
    fun wxPay(
        context: Context,
        appId: String?,
        partnerId: String?,
        prepayId: String?,
        noncestr: String?,
        timestamp: String?,
        paySign: String?,
        block: ((WxStatus, String) -> Unit) = { _, _ -> }
    ) {
        WXPayEntryActivity.block = block
        val request = PayReq().apply {
            this.appId = appId//appId
            this.partnerId = partnerId//商户号
            this.prepayId = prepayId//预支付交易会话id
            this.packageValue = "Sign=WXPay"//扩展字段,填写默认
            this.nonceStr = noncestr//随机字符串
            this.timeStamp = timestamp//时间戳
            this.sign = paySign //签名
        }
        if (context.checkInstallWx()) {
            WXAPIFactory.createWXAPI(context, appId).sendReq(request)
        }
    }


    private fun Context.checkInstallWx() =
        if (PlatformUtil.isWeChatAvailable(this)) true
        else {
            "微信客户端似乎没有安装".toast();false
        }

//    fun payCallback(errCode: Int?) {
//        when (errCode) {
//            BaseResp.ErrCode.ERR_OK -> "成功"
//            BaseResp.ErrCode.ERR_COMM -> "支付失败,详情请联系客服"
//            BaseResp.ErrCode.ERR_USER_CANCEL -> "支付已取消"
//            BaseResp.ErrCode.ERR_SENT_FAILED,
//            BaseResp.ErrCode.ERR_AUTH_DENIED,
//            BaseResp.ErrCode.ERR_UNSUPPORT,
//            BaseResp.ErrCode.ERR_BAN -> {
//                "未知错误"
//            }
//            else -> "未知错误"
//        }
//    }

    //app_id=2015052600090779
// &biz_content=%7B%22timeout_express%22%3A%2230m%22%2C%22seller_id%22%3A%22%22%2C%22product_code%22%3A%22QUICK_MSECURITY_PAY%22%2C%22total_amount%22%3A%220.02%22%2C%22subject%22%3A%221%22%2C%22body%22%3A%22%E6%88%91%E6%98%AF%E6%B5%8B%E8%AF%95%E6%95%B0%E6%8D%AE%22%2C%22out_trade_no%22%3A%22314VYGIAGG7ZOYY%22%7D
// &charset=utf-8
// &method=alipay.trade.app.pay
// &sign_type=RSA2
// &timestamp=2016-08-15%2012%3A12%3A15
// &version=1.0
// &sign=MsbylYkCzlfYLy9PeRwUUIg9nZPeN9SfXPNavUCroGKR5Kqvx0nEnd3eRmKxJuthNUx4ERCXe552EV9PfwexqW%2B1wbKOdYtDIb4%2B7PL3Pc94RZL0zKaWcaY3tSL89%2FuAVUsQuFqEJdhIukuKygrXucvejOUgTCfoUdwTi7z%2BZzQ%3D
//    suspend fun aiPay(
//        activity: Activity,
//        biz_content: String,//业务请求参数的集合，最大长度不限，除公共参数外所有请求参数都必须放在这个参数中传递
//        charset: String,//编码格式,例如utf-8
//        method: String,//接口名称,例如alipay.trade.app.pay
//        sign_type: String,//商户生成签名字符串所使用的签名算法类型，目前支持RSA2和RSA，推荐使用RSA2
//        timestamp: String,//发送请求的时间，格式"yyyy-MM-dd HH:mm:ss"
//        version: String,//调用的接口版本，固定为：1.0
//        sign: String//商户请求参数的签名串，详见签名
//    ) = withContext(Dispatchers.IO) {
//        val orderInfo = "" // 订单信息
//        PayTask(activity).payV2(orderInfo, true)
//    }

    suspend fun aiPay(activity: Activity, orderInfo: String) =
        withContext(Dispatchers.IO) { PayTask(activity).payV2(orderInfo, true) }
}